package com.quzi.kettle.activitise.process;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;

import java.util.HashMap;
import java.util.Map;

import com.example.httprequest.AsyncHttpUtilClient;
import com.loopj.android.http.RequestParams;
import com.quzi.kettle.MyApplication;
import com.quzi.kettle.R;
import com.quzi.kettle.activitise.BaseActivity;
import com.quzi.kettle.api.response.BooleanResponse;
import com.quzi.kettle.utils.StrPattern;
import com.quzi.kettle.view.ImageEditText;
import com.quzi.kettle.view.TitleBarView;

public class ForgetPasswordActivity extends BaseActivity {

	TitleBarView titleView;
	ImageEditText phone_number, authcode, password, iet_forget_password;
	Button register, getAuth;
	Handler handler;
	private int mCount = 60;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_forget);

		phone_number = (ImageEditText) findViewById(R.id.phonenumber);
		authcode = (ImageEditText) findViewById(R.id.authcode);
		password = (ImageEditText) findViewById(R.id.password);
		iet_forget_password = (ImageEditText) findViewById(R.id.iet_forget_password);

		getAuth = (Button) findViewById(R.id.getauth);
		register = (Button) findViewById(R.id.register);

		phone_number.update_image(R.drawable.mobile);
		authcode.update_image(R.drawable.verifycode);
		password.update_image(R.drawable.passcode);

		phone_number.setHint(getResources().getString(R.string.phone_number));
		authcode.setHint(getResources().getString(R.string.auth_code));
		password.setHint(getResources().getString(R.string.new_pwd));
		iet_forget_password.setHint(getResources().getString(
				R.string.confirmpassword));
		getAuth.setOnClickListener(this);
		register.setOnClickListener(this);

		initTitleBar();

		handler = new Handler() {
			@Override
			public void dispatchMessage(Message msg) {
				// TODO Auto-generated method stub
				super.dispatchMessage(msg);
				switch (msg.what) {
				case 0:
					if (mCount < 0) {
						mCount = 60;
						getAuth.setText("获取验证码");
						handler.removeMessages(0);
						return;
					}
					getAuth.setText(mCount + "");
					mCount--;
					handler.sendEmptyMessageDelayed(0, 1000);
					break;
				case 1:
					dissLoadingDialog();
					break;
				}
			}
		};
	}

	@Override
	protected void initTitleBar() {
		titleView = (TitleBarView) findViewById(R.id.titleview);
		titleView.setTitle(getResources().getString(R.string.forget_password));
	}

	private boolean hasSend = false;
	private String phoneNumber;

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.getauth:
			// 获取验证码
			if (hasSend) {
				return;
			}
			showLoadingDialog();
			phoneNumber = phone_number.getText().trim();
			String telRegex = "[1][3578]\\d{9}";
			if (!phoneNumber.isEmpty() && phoneNumber.matches(telRegex)) {
				Map<String, String> request = new HashMap<String, String>();
				request.put("phone_number", phoneNumber);
				AsyncHttpUtilClient.doPost(
						AsyncHttpUtilClient.POST,
						MyApplication.url
								+ MyApplication.requestMap
										.get(MyApplication.USER_GETCODE), null,
						new RequestParams(request),
						new BooleanResponse<Boolean>() {

							@Override
							public void onSuccess(Boolean t) {
								// TODO Auto-generated method stub
								UIHandler.obtainMessage(TOASTMSG, "发送成功")
										.sendToTarget();
								hasSend = true;
								handler.sendEmptyMessage(1);
								handler.sendEmptyMessageDelayed(0, 1000);
							}

							@Override
							public void onFailure(int arg0, String arg1) {
								hasSend = false;
								handler.sendEmptyMessage(1);
								UIHandler.obtainMessage(TOASTMSG, arg1)
										.sendToTarget();
							}
						});
			}
			break;
		case R.id.register:
			showLoadingDialog();
			phoneNumber = phone_number.getText().trim();
			String passWord = password.getText().trim();
			String confirmpswd = iet_forget_password.getText().trim();
			String authCode = authcode.getText().trim();

			if (phoneNumber.isEmpty()) {
				showTextToast("请输入手机号");
				return;
			}
			if (passWord.isEmpty() || !StrPattern.checkpswd(passWord)) {
				showTextToast("密码应为6-20位数字字母组合");
				return;
			}
			if (confirmpswd.isEmpty() || !StrPattern.checkpswd(confirmpswd)) {
				showTextToast("确认密码应为6-20位数字字母组合");
				return;
			}
			if (!passWord.equals(confirmpswd)) {
				showTextToast("密码不一致");
				return;
			}
			if (authCode.isEmpty()) {
				showTextToast("请输入验证码");
				return;
			}

			Map<String, String> request = new HashMap<String, String>();
			request.put("phone_number", phoneNumber);
			request.put("password", passWord);
			request.put("code", authCode);
			AsyncHttpUtilClient.doPost(
					AsyncHttpUtilClient.POST,
					MyApplication.url
							+ MyApplication.requestMap
									.get(MyApplication.USER_FORGETPASSPSW),
					null, new RequestParams(request),
					new BooleanResponse<Boolean>() {

						@Override
						public void onSuccess(Boolean t) {
							// TODO Auto-generated method stub
							UIHandler.obtainMessage(TOASTMSG, "修改密码成功")
									.sendToTarget();
							handler.sendEmptyMessage(1);
							ForgetPasswordActivity.this.finish();
						}

						@Override
						public void onFailure(int arg0, String arg1) {
							hasSend = false;
							handler.sendEmptyMessage(1);
							UIHandler.obtainMessage(TOASTMSG, arg1)
									.sendToTarget();
						}
					});
			break;
		}
	}

}
